﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace DMA.SGDB.DAL
{
    public class FornecedorPJDAL
    {
        public void Insert(FORNECEDOR_PJ fornecedor)
        {
            try
            {
                using(var db = new DMA_SGDBEntities())
                {
                    db.AddToFORNECEDOR_PJ(fornecedor);
                    db.SaveChanges();
                }
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        public void Update(FORNECEDOR_PJ fornecedor)
        {
            try
            {
                EntityKey key;
                object originalItem;

                using (var db = new DMA_SGDBEntities())
                {
                    key = db.CreateEntityKey("FORNECEDOR_PJ", fornecedor);

                    if (db.TryGetObjectByKey(key, out originalItem))
                    {
                        db.ApplyCurrentValues(key.EntitySetName, fornecedor);
                    }

                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Delete(int idfornecedor)
        {
            try
            {
                using (var db = new DMA_SGDBEntities())
                {
                    var recordToBeExcluded = db.FORNECEDOR_PJ.Single(f => f.COD_FOR == idfornecedor);
                    db.FORNECEDOR_PJ.DeleteObject(recordToBeExcluded);
                    db.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IList<FORNECEDOR_PJ> GetAll()
        {
            try
            {
                using (var db = new DMA_SGDBEntities())
                {
                    return db.FORNECEDOR_PJ.ToList();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public FORNECEDOR_PJ LoadById(int idFORNECEDOR_PJ)
        {
            try
            {
                using (var db = new DMA_SGDBEntities())
                {
                    return db.FORNECEDOR_PJ.Single(f => f.COD_FOR == idFORNECEDOR_PJ);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IList<FORNECEDOR_PJ> GetDuplicateRecord(FORNECEDOR_PJ fornecedorPJ)
        {
            try
            {
                using (var db = new DMA_SGDBEntities())
                {
                    if (string.IsNullOrEmpty(fornecedorPJ.CNPJ_FOR))
                        return db.FORNECEDOR_PJ.Where(r => r.CNPJ_FOR == fornecedorPJ.CNPJ_FOR).ToList();
                    else
                        return null;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public IList<FORNECEDOR_PJ> GetDuplicateDescription(FORNECEDOR_PJ FORNECEDOR_PJ)
        {
            try
            {
                using (var db = new DMA_SGDBEntities())
                {
                    //return db.FORNECEDOR_PJ.Where(r => r.IdFORNECEDOR_PJ != FORNECEDOR_PJ.IdFORNECEDOR_PJ && r.Description.ToUpper().Equals(FORNECEDOR_PJ.Description.ToUpper())).ToList();
                    return null;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
